
var notes = [];
var currentId;
function setCurrentId(id){
    currentId = id;
}

function getCurrentId(){
    return currentId;
}

function selectNoteAction(){
    if (!isWait()) {
        var noteid = this.getAttributeNode("id");
        if (isNull(noteid)) { return false; }
        
        var ind = noteid.value.indexOf('_');
        if (ind == -1) { return false; }
        
        var id = noteid.value.substr(ind + 1, noteid.value.length - ind);
        setCurrentId(id);
        
        if (noteid.value.substr(0, 7) == "actent_") {
            selectEditNote();
        }
        else if (noteid.value.substr(0, 7) == "actant_") {
            selectApplyNote();
        }
        else if (noteid.value.substr(0, 7) == "actrnt_") {
            selectRemoveNote();
        }
        setWait();
    }
}

/**
 * callback for select an edit note button
 */
function selectEditNote(){
    // TODO: add a save-string translator
    var params = 'action=edit&id=' + getCurrentId();
    sendRequest('note', params, editApplyNoteResponse);
}

/**
 * callback for select an edit note button
 */
function selectApplyNote(){
    var title = getText(document.getElementById('ntt_' + getCurrentId()));
    var text = getText(document.getElementById('ntv_' + getCurrentId()));
    // TODO: add a save-string translator
    var params = 'action=apply&id=' + getCurrentId() + '&title=' + title + '&note=' + text;
    sendRequest('note', params, editApplyNoteResponse);
}

/**
 * callback for remove an note
 */
function selectRemoveNote(){
    // TODO: add a save-string translator
    var params = 'action=remove&id=' + getCurrentId();
    sendRequest('note', params, removeNoteResponse);
}


function editApplyNoteResponse(){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == '200') {
            var notes = document.getElementById('nts');
            if (isNull(notes)) { return false; }
            
            var newNote = document._importNode(httpRequest.responseXML.documentElement, true);
            var oldNote = document.getElementById('mainnt_' + getCurrentId());
            
            if (isNull(oldNote)) { return false; }
            
            notes.replaceChild(newNote, oldNote);
            
            bodymain();
        }
        else {
            setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
        }
        cleanWait();
    }
}

function removeNoteResponse(){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == '200') {
            var oldNote = document.getElementById('mainnt_' + getCurrentId());
            if (isNull(oldNote)) { return false; }
            oldNote.parentNode.removeChild(oldNote)
            
            bodymain();
        }
        else {
            setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
        }
        cleanWait();
    }
}


function selectNewNote(){
    if (!isWait()) {
    
        var params = 'action=new';
        sendRequest('note', params, newNoteResponse);
        
        setWait();
    }
}

function newNoteResponse(){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == '200') {
            var notes = document.getElementById('nts');
            if (isNull(notes)) { return false; }
            
            var importedNode = document._importNode(httpRequest.responseXML.documentElement, true);
            notes.appendChild(importedNode);
            
            bodymain();
        }
        else {
            setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
        }
        cleanWait();
    }
}





function saveSetting(){
    
}


/**
 * initialize
 */
function bodymain(){
    // initialize menu's items
    var notes = document.getElementById("nts");
    if (notes != null) {
        var childs = notes.getElementsByTagName("div");
        if (isNull(childs)) { return false; }
        
        for (var i = 0; i < childs.length; i++) {
            var noteId = childs[i].getAttributeNode("id");
            if (isNull(noteId)) { return false; }
            if (noteId.value.substr(0, 3) == "act") {
                childs[i].onclick = selectNoteAction;
            }
        }
        document.getElementById("newnote").onclick = selectNewNote;
    }
    
    var setting = document.getElementById("actset");
    if(setting != null){
        setting.onclick = saveSetting;
    }
    
    
    
};

addEvent(window, 'load', bodymain)
